export default class common {
	constructor() {
		const Vue_ = arguments[0].prototype;
		Vue_.$moneyFormat = this.moneyFormat;
		Vue_.$Timestamp = this.Timestamp;
		Vue_.$Toast = this.Toast;
	}
	/**
	 * 金额转为千分符
	 */
	moneyFormat(num, decimalNumber) {
		if (num) {
			let string = String(num).replace(/,/g, '');
			let str = parseFloat(string).toFixed(decimalNumber || 2);
			let decimal = str.split('.')[1] || '00'; //小数部分
			let tempArr = [];
			let revNumArr = str.split('.')[0].split("").reverse(); //倒序
			for (let i in revNumArr) {
				tempArr.push(revNumArr[i]);
				if ((i + 1) % 3 === 0 && i != revNumArr.length - 1) {
					tempArr.push(',');
				}
			}
			let zs = tempArr.reverse().join(''); //整数部分
			zs = zs.replace('-,', '-');
			let money = decimal ? zs + '.' + decimal : zs;
			return money;
		} else {
			return '0.00'
		}
	}
	/**
	 *  时间戳转年月日时分秒
	 *  @param   {Number}   stamp     stamp参数  字符串或者number类型时间戳10位或者13位长度
	 *
	 */
	Timestamp(stamp, type, isC) {
		// 判断是不是13位的时间戳格式
		if (String(stamp).length < 13) {
			stamp = stamp * 1000
		}
		let date = new Date(Number(stamp)),
			year = date.getFullYear(),
			month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1),
			day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate(),
			hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours(),
			minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes(),
			second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();
		if (type == 'year') {
			return year;
		} else if (type == 'month') {
			return year + '/' + month;
		} else if (type == 'day') {
			return `${year}/${month}/${day}`;
		} else if (type == 'hour') {
			return year + '/' + month + '/' + day + ' ' + hour
		} else if (type == 'minute') {
			return year + '/' + month + '/' + day + ' ' + hour + ':' + minute
		} else if (type == 'h:m:s') {
			return hour + ':' + minute + ':' + second
		}
		return year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second
	}
	/**
	 *  Toast
	 *  @param   {msg, duration} 
	 *
	 */
	Toast(msg, duration) {
		// uni.$u.toast(msg, 5000);
		wx.showToast({
			title: msg,
			duration: 4000,
			icon: 'none'
		})
	}
}
